#!/bin/bash
#
# 4shared.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_MIGHTYUPLOAD_REGEXP_URL="http://\(www\.\)\?mightyupload\.com/*/"

MODULE_MIGHTYUPLOAD_DOWNLOAD_OPTIONS=""
MODULE_MIGHTYUPLOAD_DOWNLOAD_RESUME=yes
MODULE_MIGHTYUPLOAD_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=yes

MODULE_MIGHTYUPLOAD_LIST_OPTIONS=""

mightyupload_download() {

local COOKIEFILE=$1
local URL=$2

	Pageinit=$(curl "$URL") || return
	
    if match 'File Not Found' "$Pageinit"; then
        return $ERR_LINK_DEAD
    fi
    
       # FNAME=$(echo "$HTML" | parse_attr 'fname' 'value') || return

        log_debug 'Solve Media CAPTCHA found'

        RESP=$(solvemedia_captcha_process 'gogtPRBbytIREGnA8S2PI4v1GjwSBg8V') || return
        { read CHALL; read ID; } <<< "$RESP"

        CAPTCHA_DATA="-d adcopy_challenge=$(echo "$CHALL" | uri_encode_strict) -d adcopy_response=manual_challenge"


  values=$(echo "$Pageinit" | grep 'value="' | head -n -3)
  op=$(echo "$values" | parse_form_input_by_name 'op')
  id=$(echo "$values" | parse_form_input_by_name 'id')
  rand=$(echo "$values" | parse_form_input_by_name 'rand')
  
  HTML=$(curl -c $COOKIEFILE -e $URL $CAPTCHA_DATA -d "op=$op&id=$id&rand=$rand&referer=" $URL) || return

echo "$HTML" > html

   captcha_ack $ID
    log_debug 'Correct captcha'
    
    # Obfuscated javascript
   # echo  $HTML > vidbuxhtml
   # JS_CODE=$(echo "$HTML" | parse 'split(' '>\(.*\)$') || echo "okay"
  # JS_CODE=$(echo $HTML | sed 's/.*p,a,c,k//;s/<\/script.*//' )
  # echo $JS_CODE > jscode
  #  JS_CODE2=$(echo "eval = function(x) { print(x); }; eval(function(p,a,c,k$JS_CODE" | javascript) || return
  #  echo $JS_CODE2 > jscode2
    
  #  JS_CODE2=${JS_CODE2:222}
    # JS_CODE2=${JS_CODE2%???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
  # JS_CODE2=$(echo $JS_CODE2 | sed "s/'.*//g" )
   # FILE_URL=$( echo "$JS_CODE2" | replace "'" '"' | parse 'location' 'href' )
   # FILE_URL=$( echo "$JS_CODE2" ) 

# echo "$FILE_URL" > FILE_URL

#	downloadbtnpage=$(curl -c $COOKIEFILE -e $URL $CAPTCHA_DATA -d "op=$op&id=$id&rand=$rand&referer=" $URL) || return
	
	LINE=$(echo "$HTML" | grep 'file:')
	LINE=${LINE:10}
	LINE=${LINE%??}
	
	# echo "$LINE" > line
	
  FILE_URL="$LINE"
  
	FILENAME=$(echo $HTML | parse_tag h3)
	
	echo "$FILE_URL"
	echo "$FILENAME"
}
